package com.handpay.nocardpay.baselibrary.raindrops.model.impl;

import android.content.Context;

import com.android.volley.Request;
import com.android.volley.VolleyError;
import com.handpay.nocardpay.baselibrary.R;
import com.handpay.nocardpay.baselibrary.commbase.model.IBaseModel;
import com.handpay.nocardpay.baselibrary.commbase.model.bean.BaseErrorInfo;
import com.handpay.nocardpay.baselibrary.commbase.model.bean.BaseErrorParser;
import com.handpay.nocardpay.baselibrary.config.NoCardPayConfig;
import com.handpay.nocardpay.baselibrary.http.HttpHeadFactory;
import com.handpay.nocardpay.baselibrary.http.HttpListener;
import com.handpay.nocardpay.baselibrary.http.HttpQueueManager;
import com.handpay.nocardpay.baselibrary.http.HttpRequest;
import com.handpay.nocardpay.baselibrary.http.HttpRequestFactory;
import com.handpay.nocardpay.baselibrary.http.SSLCustomSocketFactory;
import com.handpay.nocardpay.baselibrary.logutils.HPLog;
import com.handpay.nocardpay.baselibrary.raindrops.model.IRainDropsModel;
import com.handpay.nocardpay.baselibrary.raindrops.model.bean.AuthResult;

import java.io.InputStream;
import java.util.Map;

/**
 * Created by sxshi on 2018-4-23.
 */

public class RainDropsModeImpl implements IRainDropsModel<AuthResult> {
    private Context mContext;
    private InputStream trustKeyStore;
    private HttpQueueManager httpQueueManager;
    /**
     * 请求服务器地址
     */
    private String mRequestHost;

    public RainDropsModeImpl(Context mContext) {
        this.mContext = mContext;
        mRequestHost = NoCardPayConfig.RAINDROPS_URL;
        boolean isHttps = mRequestHost.startsWith("https");
        trustKeyStore = mContext.getResources().openRawResource(R.raw.intrepaypro);
        httpQueueManager = new HttpQueueManager.Builder(mContext.getApplicationContext())
                .setSSLSocketFactory(SSLCustomSocketFactory.getSocketFactory(trustKeyStore))
                .isHttps(isHttps)
                .build();
    }

    @Override
    public void getAuthStatus(String path, Map<String, Object> paramers, Class<AuthResult> clzz, final IBaseModel.OnRequestFinishedListener<AuthResult> listener) {
        final HttpRequest request = new HttpRequest.Builder(mRequestHost + path)
                .setMethod(Request.Method.POST)
                .addheader(HttpHeadFactory.createRequestHeaders(mContext))
                .addParam(paramers)
                .build();
        Request gsonRequest = HttpRequestFactory.gsonRequest(clzz, request, new HttpListener<AuthResult>() {
            @Override
            public void onSuccess(AuthResult result) {
                if (result == null) {
                    listener.onError(new BaseErrorInfo("-1", mContext.getString(R.string.paser_error)));
                    return;
                }
                if ("00".equals(result.getStatus())) {
                    listener.onSuccess(result);
                } else {
                    listener.onError(new BaseErrorInfo(result.getStatus(), result.getMsg()));
                }
            }

            @Override
            public void onError(VolleyError error) {
                listener.onError(new BaseErrorParser(error));
            }
        });

        httpQueueManager.addRequest(gsonRequest, mRequestHost + path);
    }

    @Override
    public void noticeUpload(String path, Map<String, Object> paramers) {
        final HttpRequest request = new HttpRequest.Builder(mRequestHost + path)
                .setMethod(Request.Method.POST)
                .addheader(HttpHeadFactory.createRequestHeaders(mContext))
                .addParam(paramers)
                .build();
        Request stringRequest = HttpRequestFactory.stringRequest(request, new HttpListener<String>() {
            @Override
            public void onSuccess(String result) {
                //异步上传 不处理结果
            }

            @Override
            public void onError(VolleyError error) {

            }
        });

        httpQueueManager.addRequest(stringRequest, mRequestHost + path);
    }

    @Override
    public void onDestory() {
//       httpQueueManager.cancelRequest();
    }
}
